<!-- 求助管理页面 -->
<template>
  <div class="help-manage">
    <el-container>
      <el-header height="40px" style="text-align: center">
        <!-- Header content -->
        <div class="banner">
          <img src="../img/renzheng.png" />
          <span class="title">求助管理</span>
        </div>
      </el-header>
      <el-main height="auto">
        <!-- Main content -->
        <el-table :data="helpList" style="width: 100%">
          <el-table-column label="标题" prop="title"> </el-table-column>
          <el-table-column label="类别" prop="categoryName"> </el-table-column>
          <el-table-column label="报酬">
            <template slot-scope="scope">
              {{ scope.row.reward + "￥" }}
            </template>
          </el-table-column>
          <el-table-column label="已接人数">
            <template slot-scope="scope">
              {{ scope.row.assistNum + "/" + scope.row.needNum }}
            </template>
          </el-table-column>
          <el-table-column label="状态">
            <template slot-scope="scope">
              {{ scope.row.status == 0 ? "待接取" : "已结束" }}
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" width="250px">
            <template slot-scope="scope">
              <el-dropdown
                szie="mini"
                placement="bottom-end"
                style="margin-right: 10px"
                trigger="click"
              >
                <span class="el-dropdown-link">
                  <el-button type="text"
                    >选择动作<i class="el-icon-arrow-down el-icon--right"></i
                  ></el-button>
                </span>
                <el-dropdown-menu slot="dropdown" style="width: 96px">
                  <el-dropdown-item @click.native="goHelpDetail(scope.row.id)"
                    >查看详情</el-dropdown-item
                  >
                  <el-dropdown-item @click.native="clickUpdate(scope.row.id)"
                    >修改</el-dropdown-item
                  >
                  <el-dropdown-item
                    @click.native="deleteHelp(scope.row.id, scope.$index)"
                    >删除</el-dropdown-item
                  >
                </el-dropdown-menu>
              </el-dropdown>
              <el-popover
                placement="bottom-end"
                trigger="click"
                @show="getAssistList(scope.row.id)"
              >
                <el-table :data="assistList">
                  <el-table-column
                    property="id"
                    label="订单号"
                  ></el-table-column>
                  <el-table-column
                    property="assistantName"
                    label="协助人"
                  ></el-table-column>
                  <el-table-column
                    width="170x"
                    property="createTime"
                    label="创建时间"
                  ></el-table-column>
                  <el-table-column width="100px" property="status" label="状态">
                    <template slot-scope="scope">
                      {{ getStatus(scope.row) }}
                    </template>
                  </el-table-column>
                  <el-table-column label="操作" width="100px">
                    <template slot-scope="scope">
                      <el-button
                        :disabled="
                          scope.row.status == '2' || scope.row.status == '3'
                        "
                        @click="clickVerify(scope.row.id, scope.$index)"
                        type="text"
                        >审核</el-button
                      >
                      <el-button
                        @click="clickAssistDetail(scope.row.id, scope.$index)"
                        type="text"
                        >详情</el-button
                      >
                    </template>
                  </el-table-column>
                </el-table>

                <el-button slot="reference" size="mini">协助列表</el-button>
              </el-popover>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer height="35px" style="text-align: center">
        <!-- Footer content -->
        <el-pagination
          :page-size="params.pageSize"
          :current-page="params.pageNum"
          @current-change="changePageNum"
          layout="prev, pager, next"
          :total="total"
        >
        </el-pagination>
      </el-footer>
    </el-container>
    <assist-verify-dialog
      :title="isVerify ? '审核协助订单' : '协助详情'"
      :assistDetail="assistDetail"
      :dialogVisible="dialogVisible"
      :handleClose="handleClose"
      :isFinish="false"
      :isDetail="isDetail"
      :isVerify="isVerify"
      @infoChange="verifyInfoChange"
      @confirm="dialogVisible = false"
      @verify="verify"
    >
    </assist-verify-dialog>
  </div>
</template>

<script>
import { getHelpManageList, deleteHelpById } from "@/api/help";
import {
  getAssistListByHelp,
  getAssistDetail,
  verifyAssist,
} from "@/api/assist";
export default {
  name: "helpManagement",

  data() {
    return {
      dialogVisible: false,
      selectedIndex: null,
      assistDetail: {},
      verifyInfo: "",
      isDetail: true,
      isVerify: false,
      params: {
        pageSize: 10,
        pageNum: 1,
      },
      total: null,
      helpList: [],
      assistList: [],
    };
  },
  methods: {
    verify(status) {
      this.dialogVisible = false;
      verifyAssist({
        id: this.assistDetail.id,
        comment: this.verifyInfo,
        verify: status,
      }).then((res) => {
        this.$message({
          type: "success",
          message: "审核成功",
        });
      });
    },

    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          console.log("asdfasdfasdf");
          this.dialogVisible = false;
          done();
        })
        .catch(() => {});
    },
    clickUpdate(helpId) {
      this.$router.push("/postHelp?helpId=" + helpId);
    },
    clickAssistDetail(assistId, index) {
      this.selectedIndex = index;
      this.isVerify = false;
      this.isDetail = true;
      this.dialogVisible = true;
      getAssistDetail(assistId).then((res) => {
        this.assistDetail = res;
        console.log("协助详情：", res);
      });
    },
    clickVerify(assistId, index) {
      this.selectedIndex = index;
      this.isVerify = true;
      this.dialogVisible = true;
      this.isDetail = false;
      getAssistDetail(assistId).then((res) => {
        this.assistDetail = res;
        console.log("协助详情：", res);
      });
    },
    verifyInfoChange(info) {
      console.log("检测到info变化");
      this.verifyInfo = info;
    },
    test() {},
    getHelpList() {
      //   console.log(this.params);
      getHelpManageList(this.params).then((res) => {
        this.helpList = res.rows;
        this.total = res.total;
        // console.log(res.rows);
      });
    },
    getStatus(assist) {
      switch (assist.status) {
        case "0":
          return "待完成";
        case "1":
          return "待审核";
        case "2":
          return "审核通过";
        case "3":
          return "审核不通过";
        default:
          return "待审核";
      }
    },
    getAssistList(helpId) {
      console.log("HelpId:", helpId);
      getAssistListByHelp(helpId).then((res) => {
        this.assistList = res;
        console.log(res);
      });
    },
    changePageNum(currentPage) {
      console.log(currentPage);
      //这里不能直接设置params里的属性因为默认的代理只会代理一层
      this.$set(this.params, "pageNum", currentPage);
    },
    deleteHelp(id, index) {
      deleteHelpById(id, index).then((res) => {
        this.getHelpList();
        this.$message({
          message: "删除成功",
          type: "success",
        });
      });
    },
    goHelpDetail(helpId) {
      this.$router.push("/helpDetail?helpId=" + helpId);
    },
  },
  watch: {
    params: {
      deep: true,
      handler: function (newValue, oldValue) {
        this.getHelpList();
      },
    },
  },
  created() {
    this.getHelpList();
  },
};
</script>

<style scoped>
* {
  margin: 0px;
  padding: 0px;
}
.help-manage {
  width: 100%;
  height: 100%;
  display: flex;
}
img {
  width: 35px;
  height: 35px;
  vertical-align: middle;
}
.title {
  font-size: 25px;
  color: rgb(163, 220, 243);
  display: inline-block;
  vertical-align: middle;
}
.el-button {
  margin-top: 0px !important;
}
</style>